using System;
using System.Reflection;
using System.Collections;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace CodePlex.Diagnostics
{
    /// <summary>
    /// Provides unit testing for the <see cref="T:CodePlex.Diagnostics.ExceptionSerializer"/> class.
    /// </summary>
    [TestClass]
    public sealed class ExceptionSerializerUnitTest
    {
        #region public ExceptionSerializerUnitTest()

        /// <summary>
        /// Initializes a new instance of the <see cref="T:ExceptionSerializerUnitTest"/> class.    
        /// </summary>
        public ExceptionSerializerUnitTest()
        {

        }

        #endregion

        #region public void ExceptionSerializationUnitTest()

        /// <summary>
        /// Unit test to exercise the serialization and deserialization of <see cref="T:System.Exception"/> instances.
        /// </summary>
        [TestMethod]
        public void ExceptionSerializationUnitTest()
        {
            try
            {
                // force a DivideByZeroException to use as an Exception to be serialized and deserialized.
                for (int index = 0; index < 10; index++)
                {
                    index /= index;
                }

                Assert.Inconclusive("ExceptionSerializationUnitTest is inconclusive, failed to generate the sample DivideByZeroException.");
            }
            catch (DivideByZeroException original)
            {
                // serialize the DivideByZeroException using the ExceptionSerializer class.
                string soap = ExceptionSerializer.Serialize(original);

                // ensure that the Soap encoded Exception is not null or an empty string. 
                Assert.IsFalse(string.IsNullOrEmpty(soap), "Serialized exception is either null or an empty string.");

                DivideByZeroException deserialized = ExceptionSerializer.Deserialize(soap) as DivideByZeroException;

                // ensure that the deserialized Exception is not null, if the 'as' cast fails then 'deserialized' will unfortunately be null.
                Assert.IsNotNull(deserialized);

                Assert.AreEqual<string>(original.HelpLink, deserialized.HelpLink, "Serialized exception contains an invalid HelpLink.");
                Assert.AreEqual<string>(original.Message, deserialized.Message, "Serialized exception contains an invalid Message.");
                Assert.AreEqual<string>(original.Source, deserialized.Source, "Serialized exception contains an invalid Source.");
                Assert.AreEqual<string>(original.StackTrace, deserialized.StackTrace, "Serialized exception contains an invalid StackTrace.");
            }
        }

        #endregion
    }
}

